package com.jinmei.dao.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jinmei.dao.UserDao;
import com.jinmei.model.User;


public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

	@Override
	public User findUserbyName(String name) throws DataAccessException {
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq("name", name));
		List findByCriteria = this.getHibernateTemplate().findByCriteria(criteria);
		List<User> users = findByCriteria;
		User user = null;
		if(CollectionUtils.isNotEmpty(users)){
			user = (User)users.get(0);
		}
		return user;
	}
	
	@Override
	public void changePassWord(String newPassWord, Integer userId) throws DataAccessException{
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		criteria.add(Restrictions.eq("id", userId));
		List findByCriteria = this.getHibernateTemplate().findByCriteria(criteria);
		List<User> users = findByCriteria;
		User user = (User)users.get(0);
		user.setPassword(newPassWord);
		getHibernateTemplate().update(user);
	}
		
}
